package com.vini.web.pages;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.cookies.CookieUtils;

import com.vini.web.AppSession;

public class BasePage extends WebPage {

	private static final long serialVersionUID = 1L;
	protected static final String REMEMBER_ME_LOGIN_COOKIE = "app_login_name";
	protected static final String REMEMBER_ME_PASSWORD_COOKIE = "app_login_pwd";
	private Link<Void> homeLink;
	private AjaxLink<String> signInOutLink;
	private Link<Void> signUpLink;
	//Side Menu Links
	protected Link<Void> completedRides;
	protected Link<Void> createCircle;
	protected Link<Void> manageCircles;
	protected Link<Void> newRides;
	protected Link<Void> payment;
	protected Link<Void> pendingRides;
	protected Link<Void> taxiLinkManual;
	protected Link<Void> userPreferences;
	private Label signLabel;
	public ModalWindow modalWindow;


	public BasePage() {
		initModalWindow();
		
		addMenuLinks();
		
		// Create Side Menu Links
		createSideMenuLinks();
		
		add(homeLink);
		add(signUpLink);
		add(modalWindow);
	}

	public void addMenuLinks(){
		homeLink = new BookmarkablePageLink<Void>("homeLink", Home.class);
		signUpLink = new BookmarkablePageLink<Void>("signUp",SignUp.class);

		if(((AppSession)getSession()).getUser() == null){
			signLabel = new Label("signLabel", new Model<String>("Sign in"));
			signInOutLink = new AjaxLink<String>("signInOut") {

				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget arg0) {
					setResponsePage(SignIn.class);

				}
			};
			signInOutLink.add(signLabel);
			add(signInOutLink);
		}else{
			signUpLink.setVisible(false);
			signLabel = new Label("signLabel", new Model<String>("Sign out"));
			signInOutLink = new AjaxLink<String>("signInOut") {

				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget arg0) {
					((AppSession)getSession()).removeUser();
					CookieUtils utils = new CookieUtils();
					utils.remove(REMEMBER_ME_LOGIN_COOKIE);
					utils.remove(REMEMBER_ME_PASSWORD_COOKIE);
					setResponsePage(SignIn.class);

				}
			};
			signInOutLink.add(signLabel);
			add(signInOutLink);
		}
	}

	private void createSideMenuLinks() {
		completedRides = new BookmarkablePageLink<Void>("completedRides", CompletedRides.class);
		createCircle = new BookmarkablePageLink<Void>("createCircle", CreateCircle.class);
		manageCircles = new BookmarkablePageLink<Void>("manageCircles", ManageCircles.class);
		newRides = new BookmarkablePageLink<Void>("newRides", NewRides.class);
		payment = new BookmarkablePageLink<Void>("payment", Payment.class);
		pendingRides = new BookmarkablePageLink<Void>("pendingRides", PendingRides.class);
		taxiLinkManual = new BookmarkablePageLink<Void>("taxiLinkManual", TaxiLinkManual.class);
		userPreferences = new BookmarkablePageLink<Void>("userPreferences", UserPreferences.class);
	}



	private void initModalWindow() {
		modalWindow = new ModalWindow("modal");
		modalWindow.setTitle(Model.of("Message"));
		modalWindow.setInitialHeight(100);
		modalWindow.setInitialWidth(600);
		FeedbackPanel panel = new FeedbackPanel(modalWindow.getContentId());
		modalWindow.setContent(panel);
		//modalWindow.setCssClassName("modal.css")
	}
	
	protected Link<Void> getHomePageLink() {
		return homePageLink("home");
	}
}
